<?php

namespace App\Http\Controllers\Home;

use App\Services\Home\AttributeService;
use App\Services\Home\CommunityService;
use App\Services\Home\HandroomService;
use App\Services\Home\RegionService;
use App\Services\Home\RentalhouseService;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CommunityController extends Controller
{
    protected $service;
    protected $attrService;
    protected $regionService;
    protected $handroomService;
    protected $rentalhouseService;

    public function __construct(CommunityService $service, AttributeService $attributeService,RegionService $regionService,HandroomService $handroomService, RentalhouseService $rentalhouseService)
    {
        $this->service = $service;
        $this->attrService = $attributeService;
        $this->regionService = $regionService;
        $this->handroomService = $handroomService;
        $this->rentalhouseService = $rentalhouseService;
    }

    public function index(Request $request,$id)
    {
        // 市id
//        $id = $request->get('id');
        $areaid = isset($id)?$id:0;
        $city_info = $this->regionService->getCityInfo($areaid);
        //属性
        $area = $this->attrService->getAttributeById($areaid);

//        $property = Property::paginate(10);
        //搜索词
        $keyword = $request->get('keyword')??'';
        return view(env('DEFAULT_TEMPLATE').'.community',compact('area','areaid','city_info','keyword'));
    }

    public function data(Request $request) {
        return $this->service->data($request);
    }

    public function show($id)
    {
        $data = $this->service->getDetailById($id);
        $near = $this->service->getNearbyCommunityById($id);
        $handroom = $data->handroom->take(5);
        $rental = $data->rentalhouse->take(5);
        return view(env('DEFAULT_TEMPLATE').'.residential',compact('data','near','handroom','rental'));
    }

    /**
     * @param $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * 小区二手房
     */
    public function handRoom($id)
    {
        $data = $this->service->getSingle($id);
        // 朝向
        $direction = $this->attrService->getAttributeById(84);
        // 装修
        $renovation = $this->attrService->getAttributeById(47);
        //特色标签
        $tags = $this->attrService->getAttributeById(74);
        return view(env('DEFAULT_TEMPLATE').'.community_hand',compact('data','direction','renovation','tags'));
    }

    public function getCommunityHandRoom(Request $request)
    {
        return $this->handroomService->data($request);
    }

    /**
     * @param $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * 小区租房
     */
    public function rentalHouse($id)
    {
        $data = $this->service->getSingle($id);
        // 朝向
        $direction = $this->attrService->getAttributeById(84);
        // 装修
        $renovation = $this->attrService->getAttributeById(47);
        //特色
        $tags = $this->attrService->getAttributeById(78);
        // 房间配置
        $supporting = $this->attrService->getAttributeById(52);

        return view(env('DEFAULT_TEMPLATE').'.community_rental',compact('data','direction','renovation','tags','supporting'));
    }

    public function getCommunityRentalHouse(Request $request)
    {
        return $this->rentalhouseService->data($request);
    }


}
